# -*- coding: utf-8 -*-
#
# Author:: JonnyHan
# Date:: 2020/12/21


def strip_item_in_list(target_list, str_list):
    res_list = []
    for item in target_list:
        for item2 in str_list:
            item = item.strip(item2)
        res_list.append(item)
    return res_list


def get_text_data(filename, separator="\t", not_empty_column_index=[], ignore_first_line=True, ignore_empty_line=False):
    '''

    :param filename: 文件的绝对路径
    :param separator:
    :param not_null_column:
    :param ignore_first_line:
    :param ignore_empty_line:
    :return:
    '''
    f = open(filename, "r", encoding="utf-8")
    data_list = []
    file_list = f.readlines()

    if ignore_first_line == True:
        file_list = file_list[1:]

    if ignore_empty_line == True:
        for line in file_list:
            line_list = line.strip("\n").split(separator)
            line_list = strip_item_in_list(line_list, ["'", '"'])
            is_empty_line = True
            for item in line_list:
                if item != "":
                    is_empty_line = False
                    break
            if is_empty_line == False:
                data_list.append(line_list)
    else:
        for line in file_list:
            line_list = line.strip("\n").split(separator)
            line_list = strip_item_in_list(line_list, ["'", '"'])
            data_list.append(line_list)

    # 指定列不可为空时，过滤无效数据
    if len(not_empty_column_index) > 0:
        data_list_remove_invalid = []
        for item in data_list:
            is_invalid_line = False
            for i in not_empty_column_index:
                try:
                    if item[i] == "":
                        is_invalid_line = True
                        break
                    else:
                        pass
                except:
                    is_invalid_line = True
                    break
            if is_invalid_line == False:
                data_list_remove_invalid.append(item)
        data_list = data_list_remove_invalid

    return data_list


if __name__ == "__main__":
    import os
    from submodules.common.lib.local_paths import project_path

    file_path = os.path.join(project_path, "test", "data", "测试数据.csv")
    # r = get_text_data(file_path, ",")
    r = get_text_data(file_path, ",", not_null_column=[1, 3])
    print(r)
